package com.cqc.jdk8.Jutil;

import com.cqc.eb.PersonEB;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


public class ComparatorTest {
    @Test
    public void f1() {
        List<Integer> list = new ArrayList<>();
        list.add(10);
        list.add(1);
        list.add(5);
        list.add(8);
        Collections.sort(list, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1 - o2;
            }
        });
        //[1, 5, 8, 10]
        System.out.println(list);
    }

    @Test
    public void f2() {
        List<PersonEB> list = new ArrayList<>();
        list.add(new PersonEB(10, "刘亦菲"));
        list.add(new PersonEB(1, "张曼玉"));
        list.add(new PersonEB(1, "林青霞"));
        list.add(new PersonEB(5, "古力娜扎"));
        list.add(new PersonEB(8, "黄圣依"));
        //注意，必须是 (PersonEB p) -> p.getId()
        list.sort(
                Comparator.comparingInt((PersonEB p) -> p.getId())
                        .thenComparing(p -> p.getName(),Comparator.reverseOrder())
        );
//[PersonEB{id=1, name='林青霞'},
// PersonEB{id=1, name='张曼玉'},
// PersonEB{id=5, name='古力娜扎'},
// PersonEB{id=8, name='黄圣依'}, P
// ersonEB{id=10, name='刘亦菲'}]
        System.out.println(list);
    }

}
